package com.tieland.demo.dao;

import com.tieland.demo.entity.Organ;

import javax.persistence.*;
import javax.ejb.Stateless;
import javax.ejb.Local;
import java.sql.SQLException;
import java.util.List;

import org.jfox.entity.EntityManagerExt;
import org.jfox.entity.dao.DAOSupport;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2008-3-12
 * Time: 8:38:35
 * To change this template use File | Settings | File Templates.
 */
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = OrganDAOImpl.GET_ORGAN_BY_ID,
                query = "select"
                        + " ORGAN.ORGANNO as ORGANNO,"
                        + " ORGAN.ORGANNAME as ORGANNAME,"
                        + " ORGAN.ORGANLEVEL as ORGANLEVEL,"
                        + " ORGAN.UPORGANNO as UPORGANNO,"
                        + " ORGAN.FLAG as FLAG"
                        + " from T_ORGAN ORGAN"
                        + " where ORGAN.ORGANNO=$organno",
                resultClass = Organ.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "ORGAN")
                        }
        ),

        @NamedNativeQuery(
                name = OrganDAOImpl.INSERT_ORGAN,
                query = "insert into T_ORGAN ("
                        + " ORGANNAME,"
                        + " ORGANLEVEL,"
                        + " UPORGANNO,"
                        + " FLAG,"
                        + ") values("
                        + "$organ.getOrganName(),"
                        + "$organ.getOrganLevel(),"
                        + "$organ.getUpOrganNo(),"
                        + "$organ.getFlag()"
                        + " )",
                resultClass = Organ.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "ORGAN"),
                @QueryHint(name = "jdbc.compatible", value = "mysql,sqlserver,oracle,db2,")
                        }
        ),

        @NamedNativeQuery(
                name = OrganDAOImpl.UPDATE_ORGAN,
                query = "update T_ORGAN set"
                        + " ORGANNAME = $organ.getOrganName(),"
                        + " ORGANLEVEL = $organ.getOrganLevel(),"
                        + " UPORGANNO = $organ.getUpOrganNo(),"
                        + " FLAG =$organ.getFlag()"
                        + " where ORGANNO = $organ.getOrganNo()",
                resultClass = Organ.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "ORGAN"),
                @QueryHint(name = "jdbc.compatible", value = "mysql,sqlserver,oracle,db2,")
                        }
        ),

        @NamedNativeQuery(
                name = OrganDAOImpl.GET_ORGAN_LIST,
                query = "select"
                        + " ORGAN.ORGANNO as ORGANNO,"
                        + " ORGAN.ORGANNAME as ORGANNAME,"
                        + " ORGAN.ORGANLEVEL as ORGANLEVEL,"
                        + " ORGAN.UPORGANNO as UPORGANNO,"
                        + " ORGAN.FLAG as FLAG"
                        + " from T_ORGAN ORGAN"
                + " where FLAG <> '0'",
                resultClass = Organ.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "ORGAN"),
                @QueryHint(name = "jdbc.compatible", value = "mysql,sqlserver,oracle,db2,")
                        }
        )
                }
)

@Stateless
@Local
@SuppressWarnings("unchecked")
public class OrganDAOImpl extends DAOSupport implements OrganDAO {
    public static final String GET_ORGAN_BY_ID = "getOrganById";
    public static final String INSERT_ORGAN = "insertOrgan";
    public static final String UPDATE_ORGAN = "updateOrgan";
    public static final String GET_ORGAN_LIST = "getOrganList";

     @PersistenceContext(unitName = "TielandUnit")
    private EntityManagerExt em = null;

    protected EntityManager getEntityManager() {
        return em;
    }

    public Organ getOrgan(String organNo) throws SQLException {
        Query query = createNamedNativeQuery(GET_ORGAN_BY_ID).setParameter("organno",organNo);
        return (Organ)query.getSingleResult();
    }

    public void insertOrgan(Organ organ) throws SQLException {
        createNamedNativeQuery(INSERT_ORGAN).setParameter("organ",organ).executeUpdate();
    }

    public void updateOrgan(Organ organ) throws SQLException {
        createNamedNativeQuery(UPDATE_ORGAN).setParameter("organ",organ).executeUpdate();
    }

    public List<Organ> getOrganList() throws SQLException {
        Query query = createNamedNativeQuery(GET_ORGAN_LIST);
        return (List<Organ>)query.getResultList();
    }
}
